package beikai.com.assistant.common.util;

import beikai.com.assistant.common.exception.EleException;
import cn.hutool.core.util.StrUtil;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @program: electricity-assistant
 * @author: xiaobei
 * @create: 2021-12-26 23:30
 * @description: 数字转换工具类
 **/
public class NumberUtil {

    public static Integer yuanToFen(String yuan) {
        if (StrUtil.isBlank(yuan)) {
            return null;
        }
        if (yuan.contains(".")) {
            int spot = yuan.indexOf(".");
            int lastSpot = yuan.lastIndexOf(".");
            if (spot != lastSpot) {
                throw new EleException("金钱格式不正确,有多个小数点!");
            }
            if (yuan.length() - spot > 3) {
                throw new EleException("小数位只能保留两位小数!");
            }
        }
        return new BigDecimal(yuan).multiply(new BigDecimal("100")).intValue();
    }

    public static String fenToYuan(Integer fen) {
        if (Objects.isNull(fen)) {
            return "0";
        }
        return new BigDecimal(fen).divide(new BigDecimal("100"),2,BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String multiply(String one,String two){
       return new BigDecimal(one).multiply(new BigDecimal(two)).toString();
    }
}
